/**
 * 
 */
package com.xbjtech.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.xbjtech.model.Business;
import com.xbjtech.model.BusinessExample;
import com.xbjtech.model.BusinessExample.Criteria;
import com.xbjtech.service.BusinessService;
import com.xbjtech.util.ResponseBean;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/business")
@Slf4j
public class BusinessController {
	
	@Autowired
	BusinessService businessService;
	
	@RequestMapping("/list")
	@ResponseBody
	public List<Business> list() {
		List<Business> list = businessService.list();
		return list;
	}
	
	@RequestMapping("/listb")
	public String listb() {
		return "business/list";
	}
	@RequestMapping("/page")
	@ResponseBody
	public Map<String, Object> page(
			Business business,
			@RequestParam(required = false, defaultValue = "1") Integer page, 
			@RequestParam(required = false, defaultValue = "10")Integer rows) {
		log.info("business:{}", business);
		BusinessExample example = new BusinessExample();
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		if (null != business.getName() && !business.getName().isEmpty()) {
			criteria.andNameLike("%"+business.getName()+"%");
		}
		if (null != business.getLinkName() && !business.getLinkName().isEmpty()) {
			criteria.andLinkNameLike("%"+business.getLinkName()+"%");
		}
		if (null != business.getMobile() && !business.getMobile().isEmpty()) {
			criteria.andMobileLike("%"+business.getMobile()+"%");
		}
		if (null != business.getUsername() && !business.getUsername().isEmpty()) {
			criteria.andUsernameLike("%"+business.getUsername()+"%");
		}
		if (null != business.getLocked()) {
			criteria.andLockedEqualTo(business.getLocked());
		}
		if (null != business.getDeleted()) {
			criteria.andDeletedEqualTo(business.getDeleted());
		}
		List<Business> list = businessService.list(example, page, rows);
		PageInfo<Business> pageInfo = new PageInfo<>(list);
		Map<String, Object> map = new HashMap<>();
		map.put("total", pageInfo.getTotal());
		map.put("rows", pageInfo.getList());
		return map;
	}
	
	@PostMapping("/save")
	@ResponseBody
	public ResponseBean save(Business business) {
		log.info("business:{}", business);
		try {
			if (business.getId() == null) {
				businessService.insertBusiness(business);;
				return new ResponseBean(true, "保存成功");
			} else {
				businessService.updateBusiness(business);;
				return new ResponseBean(true, "更新成功");
			}
		} catch (Exception e) {
			log.error("BusinessController.save() throws a exception -> {}", e.getMessage());
		}
		return new ResponseBean(false, "操作失败");
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public ResponseBean delete(Long id) {
		try {
			businessService.deleteBusinessById(id);
			return new ResponseBean(true, "删除成功");
		} catch (Exception e) {
			log.error("{}", e.getMessage());
		}
		return new ResponseBean(false, "删除失败");
	}

}
